﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Develop.DTO.DevelopProjectImplement;
using AnQinFrameWorkAPI.Entity.Develop.View.DevelopProjectImplement;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.JWT;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using static AnQinFrameWorkAPI.Common.Enum.DevelopEnum;

namespace AnQinFrameWorkAPI.Service.Develop;

/// <summary>
///     开发管理-项目实施业务层
/// </summary>
public class DevelopProjectImplementService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //请求人信息
    private readonly IUserContext _userContext;

    //构造注入
    public DevelopProjectImplementService(IUserContext userContext, DatabaseContext context, IMapper mapper)
    {
        _userContext = userContext;
        _context = context;
        _mapper = mapper;
    }

    #region 获取开发管理项目实施列表

    public async Task<RequestResponseModel> GetProjectImplement(
        DevelopProjectImplementSearchDTO developProjectImplementSearchDTO)
    {
        //获取筛选
        var query = _context.DevelopProjectManager
            .ProjectTo<DevelopProjectImplementView>(_mapper.ConfigurationProvider)
            .AsNoTracking().Where(a => a.DevelopStatus == DevelopStatusEnum.已完成)
            .WhereIF(
                a => a.ProjectCode.Contains(developProjectImplementSearchDTO.Project) ||
                     a.ProjectName.Contains(developProjectImplementSearchDTO.Project),
                !string.IsNullOrEmpty(developProjectImplementSearchDTO.Project))
            .WhereIF(a => a.ImplementStatus == developProjectImplementSearchDTO.ImplementStatus,
                developProjectImplementSearchDTO.ImplementStatus != null)
            .WhereIF(a => a.CreateTime > developProjectImplementSearchDTO.StartTime,
                developProjectImplementSearchDTO.StartTime != null)
            .WhereIF(a => a.CreateTime < developProjectImplementSearchDTO.EndTime,
                developProjectImplementSearchDTO.EndTime != null)
            .Where(a => a.IsDelete == false);

        //返回列表
        var developProjectImplement = await query
            .OrderBy(a => a.CreateTime)
            .Page(developProjectImplementSearchDTO.PageIndex, developProjectImplementSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<DevelopProjectImplementView>(developProjectImplement, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 开发管理项目实施 - 开始实施

    public async Task<RequestResponseModel> StartDevelopProjectImplement(SingleIdDTO singleIdDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectManager, singleIdDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("项目Id无效");
        ;

        if (query.ImplementStatus != ImplementStatusEnum.待反馈)
            return RequestResponse.BadRequest("只有状态为[待反馈]的项目开发单才可以开始实施");

        query.ImplementStatus = ImplementStatusEnum.待实施;

        //更新操作
        _context.DevelopProjectManager.Update(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(singleIdDTO.Id, "开始实施成功");
    }

    #endregion

    #region 开发管理项目实施 - 结束实施

    public async Task<RequestResponseModel> FinishDevelopProjectImplement(SingleIdDTO singleIdDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectManager, singleIdDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("项目Id无效");
        ;

        if (query.ImplementStatus != ImplementStatusEnum.待实施)
            return RequestResponse.BadRequest("只有状态为[待实施]的项目开发单才可以完成实施");

        query.ImplementStatus = ImplementStatusEnum.已完成;

        //更新操作
        _context.DevelopProjectManager.Update(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(singleIdDTO.Id, "完成实施成功");
    }

    #endregion

    #region 获取开发管理项目需求开发列表

    public async Task<RequestResponseModel> GetProjectImplementDemand(
        DevelopProjectDemandImplementSearchDTO developProjectDemandImplementSearchDTO)
    {
        //获取筛选
        var query = _context.DevelopProjectDemand
            .ProjectTo<DevelopProjectDemandImplementView>(_mapper.ConfigurationProvider)
            .AsNoTracking().Where(a => a.ProjectId == developProjectDemandImplementSearchDTO.ProjectId)
            .WhereIF(
                a => a.Code.Contains(developProjectDemandImplementSearchDTO.Demand) ||
                     a.Name.Contains(developProjectDemandImplementSearchDTO.Demand),
                !string.IsNullOrEmpty(developProjectDemandImplementSearchDTO.Demand))
            .WhereIF(
                a => a.ProjectCode.Contains(developProjectDemandImplementSearchDTO.Project) ||
                     a.ProjectName.Contains(developProjectDemandImplementSearchDTO.Project),
                !string.IsNullOrEmpty(developProjectDemandImplementSearchDTO.Project))
            .WhereIF(a => a.Content.Contains(developProjectDemandImplementSearchDTO.Content),
                !string.IsNullOrEmpty(developProjectDemandImplementSearchDTO.Content))
            .WhereIF(a => a.ImplementStaffName.Contains(developProjectDemandImplementSearchDTO.DevelopStaffName),
                !string.IsNullOrEmpty(developProjectDemandImplementSearchDTO.DevelopStaffName))
            .WhereIF(a => a.ImplementItemStatus == developProjectDemandImplementSearchDTO.ImplementItemStatus,
                developProjectDemandImplementSearchDTO.ImplementItemStatus != null)
            .Where(a => a.IsDelete == false);

        //返回列表
        var developProjectImplementDemand = await query
            .OrderBy(a => a.CreateTime)
            .Page(developProjectDemandImplementSearchDTO.PageIndex, developProjectDemandImplementSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<DevelopProjectDemandImplementView>(developProjectImplementDemand, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 开发管理项目需求实施 - 测试

    public async Task<RequestResponseModel> TestArrayDevelopProjectDemandImplement(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.DevelopProjectDemand
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("项目Id无效");

        if (query.Any(a => a.ImplementItemStatus != ImplementItemStatusEnum.待测试))
            return RequestResponse.BadRequest("只有待测试的项目需求才能测试");

        foreach (var item in query) item.ImplementItemStatus = ImplementItemStatusEnum.待发布;
        ;

        //更新并保存
        _context.DevelopProjectDemand.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "测试成功");
    }

    #endregion

    #region 开发管理项目需求实施 - 发布

    public async Task<RequestResponseModel> ReleaseArrayDevelopProjectDemandImplement(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.DevelopProjectDemand
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("项目Id无效");

        if (query.Any(a => a.ImplementItemStatus != ImplementItemStatusEnum.待发布))
            return RequestResponse.BadRequest("只有待发布的项目需求才能发布");

        foreach (var item in query) item.ImplementItemStatus = ImplementItemStatusEnum.待实施;
        ;

        //更新并保存
        _context.DevelopProjectDemand.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "发布成功");
    }

    #endregion

    #region 开发管理项目需求实施 - 完成

    public async Task<RequestResponseModel> FinishArrayDevelopProjectDemandImplement(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.DevelopProjectDemand
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("项目Id无效");

        if (query.Any(a => a.ImplementItemStatus != ImplementItemStatusEnum.待实施))
            return RequestResponse.BadRequest("只有待实施的项目需求才能完成实施");

        foreach (var item in query) item.ImplementItemStatus = ImplementItemStatusEnum.已完成;
        ;

        //更新并保存
        _context.DevelopProjectDemand.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "完成需求成功");
    }

    #endregion
}